package AI.screens
{
	import AI.data.EntityTypes;
	
	import com.cheezeworld.GameFactory;
	import com.cheezeworld.entity.*;
	import com.cheezeworld.math.Vector2D;
	import com.cheezeworld.screens.AScreen;
	import com.cheezeworld.screens.IScreenItem;
	import com.cheezeworld.screens.Root;
	import com.urbansquall.ui.CustomTextField;
	import com.urbansquall.ui.RectImg;
	import com.urbansquall.ui.TextButton;
	
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;

	public class AIDemoScreen extends AScreen
	{
		public function set screenLabel( a_label:String ) : void
		{
			var label:CustomTextField = getChildByName( "label" ) as CustomTextField;
			label.center();
			label.text = a_label;			
			label.x = Root.WIDTH / 2 - label.width / 2;
		}
		
		public function AIDemoScreen(a_parentScreen:IScreenItem=null)
		{
			super(a_parentScreen);
			
			// Setup Buttons and Labels --
			var label:CustomTextField = new CustomTextField( "Give Me A Name!", 0xFFFFFF );
			label.filters = [ new GlowFilter( 0x000000) ];
			label.x = Root.WIDTH / 2 - label.width / 2;
			label.y = 5;
			label.name = "label";
			addChild( label );
			
			var nextBtn:TextButton = new TextButton( new RectImg( 150, 25, 0x2222AA ), "Next Demo" );
			nextBtn.x = Root.WIDTH - nextBtn.width - 10;
			nextBtn.y = Root.HEIGHT - nextBtn.height - 5;
			nextBtn.name = "next";
			nextBtn.addEventListener( MouseEvent.CLICK, onClick );
			addChild( nextBtn );
			
			var prevBtn:TextButton = new TextButton( new RectImg( 150, 25, 0x2222AA ), "Prev Demo" );
			prevBtn.x = 10;
			prevBtn.y = Root.HEIGHT - prevBtn.height - 5;
			prevBtn.name = "prev";
			prevBtn.addEventListener( MouseEvent.CLICK, onClick );
			addChild( prevBtn );
			
			// Setup gameworld / factory
			_factory = new GameFactory();
			EntityTypes.register( _factory );
			_gameworld = _factory.createGameworld( new Vector2D( 550, 400 ), new Vector2D( 550, 400 ), this );
			
		}
		
		public override function update(a_timePassed:int):void
		{
			_gameworld.update( a_timePassed );
		}
		
		public override function dispose() : void
		{
			getChildByName( "next" ).removeEventListener( MouseEvent.CLICK, onClick );
			getChildByName( "prev" ).removeEventListener( MouseEvent.CLICK, onClick );
			_gameworld.dispose();
			_factory.dispose();
			_gameworld = null;
			_factory = null;
		}
		
		// -- PRIVATE --
		
		private function onClick( e:MouseEvent ) : void
		{
			switch( e.currentTarget.name )
			{
				case "next":
					setScreen( Main.nextScreen );
					break;
				
				case "prev":
					setScreen( Main.prevScreen );
					break;
			}
		}
		
		protected var _gameworld:GameWorld;
		protected var _factory:GameFactory;
		
		
	}
}